<?php

namespace app\model\user;

use app\base\BaseModel;
use app\model\store\StoreModel;

/**
 * 模型-GoodsCartModel
 * Class GoodsCartModel
 * @package app\model
 */
class UserCartModel extends BaseModel
{

    protected $name = 'user_cart';

    /**
     * 获取列表
     * @param $user_id
     * @param int $type
     * @param bool $is_lose
     * @param int $member_level
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getListById($user_id, $type = 0, $is_lose = false, $member_level, $goods_id = '')
    {
        $cart = $this->alias("a")
            ->leftJoin("t_goods g", "a.goods_id=g.id")
            ->leftJoin("t_goods_type b", "g.goods_type_id=b.id")
            ->leftJoin("t_goods_common c", "a.attribute_ids=c.attribute_ids")
            ->where("a.user_id", $user_id)
            ->where("a.type", $type)
            ->where('b.status', 0)
            ->where('g.is_member', '<=', $member_level)
            ->where("g.delete_time", 0)
            ->where("g.goods_status", 1)
            ->where('a.goods_number', '>', 0)
            ->order("a.update_time desc")
            ->field("a.*,g.goods_title,g.store_id,c.goods_image,c.attribute_names,c.goods_sell_price,c.goods_storage,c.goods_market_price")
            ->select();
        $cart_list = [];
        $cart_attribute_ids = [];
        foreach ($cart as $key => $value) {
            $value['selected'] = false;
            $cart_list[$value['store_id']]['list'][] = $value;
            $cart_attribute_ids[] = $value['attribute_ids'];
        }
        $cart_list_return = [];
        $cat_sum = 0;
        $store_model = new StoreModel();
        foreach ($cart_list as $key => $value) {
            $value['store'] = $store_model->getStoreInfo($key, 'id,store_name,store_label');
            $value['selected'] = false;
            $cat_sum += count($value['list']);
            $cart_list_return[] = $value;
        }
        if ($is_lose) {
            $cart_list = [];
            $cart_lose_list_return = [];
            foreach ($cart_list as $key => $value) {
                $value['store'] = $store_model->getStoreInfo($key, 'id,store_name,store_label');
                $value['selected'] = false;
                $cart_lose_list_return[] = $value;
            }
            $cart_list = self::getCartNumArr($user_id, $goods_id);
            $return_data = [
                'cart_goods_list' => $cart_list,
                'cart_list' => $cart_list_return,
                'cat_sum' => $cat_sum,
                'cart_lose_list' => $cart_lose_list_return
            ];
            return $return_data;
        }
        return $cart_list_return;
    }

    /**
     * 获取购物车商品数量
     * @param $user_id
     * @param $goods_id
     * @param $attribute_ids
     * @return int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCartNum($user_id, $goods_id, $attribute_ids)
    {
        $map = [
            'user_id' => $user_id,
            'goods_id' => $goods_id,
            'attribute_ids' => $attribute_ids,
        ];
        $cart = self::where($map)->field('goods_number')->find();
        if (empty($cart)) {
            return 0;
        }
        return $cart['goods_number'];
    }

    /**
     * 根据商品id和用户id获取购物车商品数量
     * @param $user_id
     * @param $goods_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCartNumArr($user_id, $goods_id)
    {
        if (empty($goods_id)) {
            return [];
        }
        $map = [
            'user_id' => $user_id,
            'goods_id' => $goods_id
        ];
        $cart_list = self::where($map)->field('attribute_ids,goods_number')->select();
        if (empty($cart_list)) {
            return [];
        }
        $cart_arr = [];
        foreach ($cart_list as $value) {
            $cart_arr[$value['attribute_ids']] = $value['goods_number'];
        }
        return $cart_arr;
    }

    /**
     * 添加到购物车
     * @param $user_id
     * @param $goods_id
     * @param $attribute_ids
     * @param $type
     * @param $goods_number
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addCart($user_id, $goods_id, $attribute_ids, $type = 0, $goods_number = 1)
    {
        $type = empty($type) ? 0 : $type;
        $map = [
            'user_id' => $user_id,
        ];
        $count = $this->where($map)->count();
        if ($count > 99) {
            throw new \think\Exception("购物车添加已上限", 0);
        }
        $map['goods_id'] = $goods_id;
        $map['attribute_ids'] = $attribute_ids;
        $map['type'] = $type;
        $cart = $this->where($map)->find();
        if (isset($cart['id'])) {
            //存在加一
            $up_cart['goods_number'] = $goods_number;
            $up_cart['update_time'] = time();
            $res = $this->where($map)->update($up_cart);
        } else {
            $add_cart['user_id'] = $user_id;
            $add_cart['goods_id'] = $goods_id;
            $add_cart['attribute_ids'] = $attribute_ids;
            $add_cart['type'] = $type;
            $add_cart['goods_number'] = $goods_number;
            $add_cart['update_time'] = time();
            $res = $this->save($add_cart);
        }
        $rsps['code'] = $res ? 1 : 0;
        return $rsps;
    }

    /**
     * 改变购物车数量
     * @param $id
     * @param $user_id
     * @param $goods_number
     * @param $attribute_ids
     * @param $type
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function changeCart($id, $user_id, $goods_number, $type = 0)
    {
        $type = empty($type) ? 0 : $type;
        $map = [
            'id' => $id,
            'type' => $type,
            'user_id' => $user_id
        ];
        $cart = $this->where($map)->find();
        if (!isset($cart['id'])) {
            throw new \think\Exception("数据不存在", 0);
        }
        $up_card['id'] = $id;
        $up_card['goods_number'] = $goods_number;
        $up_card['update_time'] = time();
        $res = $this->where($map)->update($up_card);
        $rsps['code'] = $res ? 1 : 0;
        return $rsps;
    }

    /**
     * 删除购物车商品
     * @param $id
     * @param $user_id
     * @param int $type
     * @return mixed
     * @throws \Exception
     */
    public function deleteCart($id, $user_id, $type = 0)
    {
        $type = empty($type) ? 0 : $type;
        $map = [
            'user_id' => $user_id,
            'type' => $type,
        ];
        $cart_model = $this->where($map);
        //如果字符串有,表示多个
        if (strpos($id, ',') > 0) {
            $cart_model->whereIn('id', $id);
        } else {
            $cart_model->where('id', $id);
        }
        $res = $cart_model->delete();
        $rsps['code'] = $res ? 1 : 0;
        return $rsps;
    }

    /**
     * 删除购物车商品
     * @param $goods_id
     * @param $attribute_ids
     * @param $user_id
     * @param $type
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function deleteCartByGoods($goods_id, $attribute_ids, $user_id, $type = 0)
    {
        $type = empty($type) ? 0 : $type;
        $map = [
            'type' => $type,
            'user_id' => $user_id,
        ];
        $cart_model = $this->where($map);
        if (is_array($goods_id)) {
            if (count($goods_id) > 1) {
                $cart_model->whereIn('goods_id', implode(",", $goods_id));
                $cart_model->whereIn('attribute_ids', implode(",", $attribute_ids));
            } else {
                $cart_model->where('goods_id', $goods_id[0]);
                $cart_model->where('attribute_ids', $attribute_ids[0]);
            }
        } else {
            $cart_model->where('goods_id', $goods_id);
            $cart_model->where('attribute_ids', $attribute_ids);
        }
        $cart = $cart_model->find();
        if (empty($cart)) {
            $rsps['code'] = 0;
            $rsps['msg'] = "数据不存在";
            return $rsps;
        }
        $res = $cart_model->delete();
        $rsps['code'] = $res ? 1 : 0;
        return $rsps;
    }
}
